﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common;
using System.Data;
[assembly: TagPrefix("FeeMgrCom", "UserControl")]
/*
 * web.config中			
 * <controls>
        <add tagPrefix="UserControls"  namespace="FeeMgrCom"></add>
 */
namespace FeeMgrCom
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:DropDownListBox runat=server></{0}:DropDownListBox>")]
    public class DropDownListBox : DropDownList
    {
        protected DataSet ds = null;
         
        private String _KindFieldName = "CarryForward_Type";

        [Bindable(false),
        Category("Data"),
        DefaultValue("CarryForward_Type")]
        public string KindFieldName
        {
            get { return _KindFieldName; }
            set { _KindFieldName = value; }
        }


        public DropDownListBox()
        {
            //
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
        }

        public void SetValue(string _Value)
        {
            if (Items.IndexOf(Items.FindByValue(_Value)) < 0)
            {
                Items.Add(_Value);
            }
            this.SelectedValue = _Value;
        }

        public void setdropdownkindfield(string fieldname)
        {
            DataView dvTree = new DataView(ds.Tables[0]);
            String ItemToAdd;
            foreach (DataRowView Row in dvTree)
            {
                ItemToAdd=Row["LISTNAME"].ToString();
                if  (Items.IndexOf( Items.FindByValue(ItemToAdd))<0)
                {
                    Items.Add(ItemToAdd);
                }
            }
        }
        protected override void OnDataBinding(EventArgs e)
        {
            base.OnDataBinding(e);
            setdropdownkindfield(KindFieldName);
        }
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            String Sql;
            if (KindFieldName != string.Empty)
            {
                Sql = "sElect ";
                Sql += " adm_dropdownlist.listname from adm_dropdownkind ,adm_dropdownlist ";
                Sql += " where adm_dropdownkind.pdropdownkindid=adm_dropdownlist.pdropdownkindid ";
                Sql += " and upper( adm_dropdownkind.kindfieldname)=upper('" + KindFieldName + "')";
                ds = DbHelperSQL.Query(Sql);
                if (this.DataSourceID.ToString() == String.Empty)
                {
                    setdropdownkindfield(KindFieldName);
                }
            }
        }
    }
}